﻿(function ($) {
    window.top.myClose;
    $.showDialoge = function (opt) {
        window.top.myClose = function () {
            $(parentDiv).empty();
        };
        opt = $.extend(true,
        {
            height: 288,    //定义高度
            width: 400,    //定义宽度
            src: "",      //内容地址
            renderTo: "showDialogeDiv", //默认主页面,如果不传父级DIV，则自动创建
            isScroll: "auto", //是否滚动条
            title: "弹出窗口样式",  //定义标题
            isShow: true,      //是否显示最小化按钮
            content: "",        //默认为空
            isIfr: true        //true 为Iframe  flase 为div
        }, opt);
        var parentDiv;
        if (opt.renderTo.match("showDialogeDiv")) {
            parentDiv = document.getElementById(opt.renderTo);
        } else {
            var parentDivTemp = '<div style="position: absolute; z-index: 3000"></div>';
            parentDiv = $(parentDivTemp).appendTo(window.document.body).get(0);
        }
        $(parentDiv).empty();
        (function () {
            var $dialogeDiv = $(document.createElement("div"));
            $dialogeDiv.attr("id", "rjjm"); //整个div加 id，用来在调用div的页面 控制 div的显示隐藏
            var tTemp_Width = opt.width - 85, cTemp_Width = opt.width - 20, cTemp_Height = opt.height - 44, cTemp_DivHeight = cTemp_Height - 40;
            $dialogeDiv.addClass("rjjm_tanchu").css({ "height": opt.height, "width": opt.width });

            var titleTemp = ['<div class="rjjm_tanchu_xiaotubiao" id="rjjm_tanchu_xiaotubiao" style="width:', tTemp_Width, 'px">\
                               <img src="jclient/images/rjjm_tanchu_img.png"/><span>', opt.title, '</span></div>'].join('');
            var btnTemp = '<div class="rjjm_tanchu_right_btn">\
                              <input type="button" class="rjjm_tanchu_btn_zuidahua" id="rjjm_tanchu_btn_zuidahua"/>\
                              <input type="button" class="rjjm_tanchu_btn_guanbi" id="rjjm_tanchu_btn_guanbi"/></div>';
            var contentTemp;
            if (opt.isIfr) {
                contentTemp = ['<div class="rjjm_tanchu_search" style="width:', cTemp_Width, 'px;height:', cTemp_Height, 'px ">\
                                <div class="rjjm_tanchu_search_div" id="rjjm_tanchu_search_div" style="width:', cTemp_Width, 'px;height:', cTemp_Height, 'px ">\
                                <iframe src="', opt.src, '" width="100%" scrolling="', opt.isScroll, '" height="100%" frameborder="0" \
                                 name="dialogeFrame" id="dialogeFrame" allowtransparency="true"></iframe></div></div>'].join('');
            }
            else {
                contentTemp = ['<div class="rjjm_tanchu_search" style="width:', cTemp_Width, 'px;height:', cTemp_Height, 'px ">\
                                <div class="rjjm_tanchu_search_div" id="rjjm_tanchu_search_div" style="width:', cTemp_Width, 'px;height:', cTemp_Height, 'px ">\
                                ' + opt.content + '</div></div>'].join('');
            }

            $dialogeDiv.append(titleTemp).append(btnTemp).append(contentTemp).appendTo(parentDiv);
            $dialogeDiv.fadeIn(1000);
        })();
        if (opt.isShow) {
            $("#rjjm_tanchu_btn_zuidahua").mouseover(function () {
                this.className = 'rjjm_tanchu_btn_zuixiaohua';
            }).mouseout(function () {
                this.className = 'rjjm_tanchu_btn_zuidahua'
            }).show();
        } else {
            $("#rjjm_tanchu_btn_guanbi").css({ "margin-left": 28 });
            $("#rjjm_tanchu_btn_zuidahua").hide();
        }
        $("#rjjm_tanchu_btn_guanbi").mouseover(function () {
            this.className = 'rjjm_tanchu_btn_guanbi_01';
        }).mouseout(function () {
            this.className = 'rjjm_tanchu_btn_guanbi';
        }).click(function () {
            $(parentDiv).empty();
        });

        var mouseStart = {};
        var divStart = {};
        var rightStart = {};
        var bottomStart = {};

        function dragMove(drag_top, oDiv) {
            if (drag_top != null) {
                var drag_top = drag_top;
                var oDiv = oDiv;
                drag_top.onmousedown = function (ev) {
                    var oEvent = ev || event;
                    mouseStart.x = oEvent.clientX;
                    mouseStart.y = oEvent.clientY;
                    divStart.x = oDiv.offsetLeft;
                    divStart.y = oDiv.offsetTop;

                    if (drag_top.setCapture) {
                        drag_top.onmousemove = doDragMove;
                        drag_top.onmouseup = stopDragMove;
                        drag_top.setCapture();

                    }
                    else {
                        document.addEventListener("mousemove", doDragMove, true);
                        document.addEventListener("mouseup", stopDragMove, true);
                    }
                };
            }
            function doDragMove(ev) {
                var oEvent = ev || event;
                var l = oEvent.clientX - mouseStart.x + divStart.x;
                var t = oEvent.clientY - mouseStart.y + divStart.y;
                if (l < 0) {
                    //l = 0;
                }
                else if (l > document.documentElement.clientWidth - oDiv.offsetWidth) {
                    //l = document.documentElement.clientWidth - oDiv.offsetWidth;
                }
                if (t < 0) {
                    //t = 0;
                }
                else if (t > document.documentElement.clientHeight - oDiv.offsetHeight) {
                    // t = document.documentElement.clientHeight - oDiv.offsetHeight;
                }
                oDiv.style.left = l + "px";
                oDiv.style.top = t + "px";
            };
            function stopDragMove() {
                if (drag_top.releaseCapture) {
                    drag_top.onmousemove = null;
                    drag_top.onmouseup = null;
                    drag_top.releaseCapture();
                }
                else {
                    document.removeEventListener("mousemove", doDragMove, true);
                    document.removeEventListener("mouseup", stopDragMove, true);
                }
            }
        }
        dragMove(document.getElementById("rjjm_tanchu_xiaotubiao"), parentDiv);
        $(parentDiv).css({ "left": ($(window).width() - $(parentDiv).width()) / 2, "top": ($(window).height() - $(parentDiv).height()) / 2 });
        //刷新iframe内容
        $.refreshDialoge = function (url) {
            document.getElementById("dialogeFrame").src = url;
        }
        $.dialogeClose = function () {
            $(document.getElementById("showDialogeDiv")).empty();
        }
    }
})(jQuery);
